﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abp.Application.Services.Dto;
using Abp.Domain.Repositories;
using Abp.Extensions;
using Abp.Linq.Extensions;
using AutoMapper;
using JHT.ABPLearn.Tasks.Dto;
using Abp.Collections.Extensions;
using Abp.E
using Abp.AutoMapper;

namespace JHT.ABPLearn.Tasks
{
    public class AbpTaskAppService : ABPLearnAppServiceBase, IAbpTaskAppService
    {
        public readonly IRepository<ABPTasks> _taskRepository;


        public AbpTaskAppService(IRepository<ABPTasks> taskRepository)
        {
            _taskRepository = taskRepository;
        }
        public int CreateTask(CreateTaskInput input)
        {
            var inputEntity = AutoMapper.Mapper.Map<ABPTasks>(input);
            return _taskRepository.Insert(inputEntity).Id;
        }

        public void DeleteTask(int taskId)
        {
            _taskRepository.Delete(taskId);
        }

        public IList<TaskDto> GetAllTasks()
        {
            var entities = _taskRepository.GetAllList();
            return Mapper.Map<List<TaskDto>>(entities);
        }

        public TaskDto GetTaskById(int taskId)
        {
            var entity = _taskRepository.Get(taskId);
            return entity.Map<TaskDto>();
        }

        public async Task<TaskDto> GetTaskByIdAsync(int taskId)
        {
            var task = await _taskRepository.GetAsync(taskId);
            return Mapper.Map<TaskDto>(task);
        }

        /// <summary>
        /// 使用Abp.linq.extensions 可以获取如下的扩展方法
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>
        public GetTasksOutput GetTasks(GetTasksInput inputDto)
        {
            var query = _taskRepository.GetAll()
                .WhereIf(inputDto.AssignedPersonId.HasValue, p => p.AssignedPersonId == inputDto.AssignedPersonId.Value)
                .WhereIf(inputDto.State.HasValue, p => p.State == inputDto.State.Value);
            GetTasksOutput output = new GetTasksOutput();
            output.Tasks = query.ToList().Map<List<TaskDto>>();
            return output;
        }


        public PagedResultDto<TaskDto> GetPagedTask(GetTasksInput input)
        {
            var query = _taskRepository.GetAll().Include(prop=>prop.AssignedPerson)
                .WhereIf(input.State.HasValue, t => t.State == input.State.Value)
                .WhereIf(!input.Filter.IsNullOrEmpty(), t => t.Title.Contains(input.Filter))
                .WhereIf(input.AssignedPersonId.HasValue, t => t.AssignedPersonId == input.AssignedPersonId.Value);

            //排序
            query = !string.IsNullOrEmpty(input.Sorting) ? query.OrderBy(p => p.CreationTime) : query.OrderByDescending(t => t.CreationTime);

            //获取总数
            var tasksCount = query.Count();
            //默认的分页方式
            //var taskList = query.Skip(input.SkipCount).Take(input.MaxResultCount).ToList();

            //ABP提供了扩展方法PageBy分页方式
            var taskList = query.PageBy(input).ToList();

            return new PagedResultDto<TaskDto>(tasksCount, taskList.MapTo<List<TaskDto>>());


        }

        /// <summary>
        /// 实体是被跟踪的，且abp在方法外会自动调用saveChanges方法
        /// </summary>
        /// <param name="input"></param>
        public void UpdateTask(UpdateTaskInputDto input)
        {
            var inputDto = input.Map<TaskDto>();

            var entiy = _taskRepository.Get(input.Id);
            if (input.State.HasValue)
            {
                entiy.State = input.State.Value;
            }

            this.UnitOfWorkManager.Current.SaveChanges();//主动调用保存更新
        }

    }
}
